#include "LightManager.h"

	LightManager::LightManager( void )
	{
		Init();
	}

	LightManager::~LightManager( void )
	{
		// delete lights from memory
		for(unsigned int i=0; i<DirectionalLights.size(); i++)
			delete DirectionalLights[i];

		for(unsigned int i=0; i<PointLights.size(); i++)
			delete PointLights[i];

		for(unsigned int i=0; i<SpotLights.size(); i++)
			delete SpotLights[i];
	}

	void LightManager::Init()
	{
		// enable lights
		SimpleRender::Get().g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

		// enable specular lighting
		SimpleRender::Get().g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );

		// Turn on ambient lighting 
		SetAmbientColour(1,1,1);
	}

	int LightManager::GetTotalLightNum()
	{
		int lightNum = DirectionalLights.size() + PointLights.size() + SpotLights.size();

		return lightNum;
	}

	void LightManager::RegisterLight( Light *light )
	{
		int lightIndex = GetTotalLightNum();

		// register light on graphics card
		SimpleRender::Get().g_pd3dDevice->SetLight( lightIndex, &light->light );

		// turn it on
		SimpleRender::Get().g_pd3dDevice->LightEnable( lightIndex, TRUE );
	}

	// set ambient light colour of scene
	void LightManager::SetAmbientColour( float r, float g, float b )
	{
		AmbientColour = D3DXCOLOR(r,g,b,1);

		SimpleRender::Get().g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, AmbientColour );
	}

